from .client import BaseSocketClient
from .client import update_last_ack_time
from .parser.ext.ex_get_history_instrument_bars_range import \
    GetHistoryInstrumentBarsRange
from .parser.ext.ex_get_history_minute_time_data import \
    GetHistoryMinuteTimeData
from .parser.ext.ex_get_history_transaction_data import \
    GetHistoryTransactionData
from .parser.ext.ex_get_instrument_bars import GetInstrumentBars
from .parser.ext.ex_get_instrument_count import GetInstrumentCount
from .parser.ext.ex_get_instrument_info import GetInstrumentInfo
from .parser.ext.ex_get_instrument_quote import GetInstrumentQuote
from .parser.ext.ex_get_instrument_quote_list import GetInstrumentQuoteList
from .parser.ext.ex_get_markets import GetMarkets
from .parser.ext.ex_get_minute_time_data import GetMinuteTimeData
from .parser.ext.ex_get_transaction_data import GetTransactionData
from .parser.ext.ex_setup_commands import ExSetupCmd1


class TdxExHq_API(BaseSocketClient):

    def setup(self):
        ExSetupCmd1(self.client).call_api()

    @update_last_ack_time
    def get_markets(self):
        """失效"""
        cmd = GetMarkets(self.client)

        return cmd.call_api()

    @update_last_ack_time
    def get_instrument_count(self):
        """
        可用
        :return:
        """
        cmd = GetInstrumentCount(self.client)

        return cmd.call_api()

    @update_last_ack_time
    def get_instrument_quote(self, market, code):
        """
        失效
        :param market:
        :param code:
        :return:
        """
        cmd = GetInstrumentQuote(self.client)
        cmd.setParams(market, code)

        return cmd.call_api()

    @update_last_ack_time
    def get_instrument_bars(self, category, market, code, start=0, count=700):
        cmd = GetInstrumentBars(self.client)
        cmd.setParams(category, market, code, start=start, count=count)

        return cmd.call_api()

    @update_last_ack_time
    def get_minute_time_data(self, market, code):
        cmd = GetMinuteTimeData(self.client)
        cmd.setParams(market, code)

        return cmd.call_api()

    @update_last_ack_time
    def get_history_minute_time_data(self, market, code, date):
        cmd = GetHistoryMinuteTimeData(self.client)
        cmd.setParams(market, code, date=date)

        return cmd.call_api()

    @update_last_ack_time
    def get_transaction_data(self, market, code, start=0, count=1800):
        cmd = GetTransactionData(self.client)
        cmd.setParams(market, code, start=start, count=count)

        return cmd.call_api()

    @update_last_ack_time
    def get_history_transaction_data(self, market, code, date, start=0, count=1800):
        cmd = GetHistoryTransactionData(self.client)
        cmd.setParams(market, code, date, start=start, count=count)

        return cmd.call_api()

    @update_last_ack_time
    def get_history_instrument_bars_range(self, market, code, start, end):
        cmd = GetHistoryInstrumentBarsRange(self.client)
        cmd.setParams(market, code, start, end)

        return cmd.call_api()

    @update_last_ack_time
    def get_instrument_info(self, start, count=100):
        """可用"""
        cmd = GetInstrumentInfo(self.client)
        cmd.setParams(start, count)

        return cmd.call_api()

    @update_last_ack_time
    def get_instrument_quote_list(self, market, category, start=0, count=80):
        cmd = GetInstrumentQuoteList(self.client)
        cmd.setParams(market, category, start, count)

        return cmd.call_api()

    def do_heartbeat(self):
        self.get_instrument_count()
